package lumis.portal.presentation.perspective.admin;

import java.util.Locale;

import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.channel.ChannelConfig;
import lumis.portal.localization.ILocalizationManager;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.page.PageConfig;
import lumis.portal.presentation.core.LayoutFilePage;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.PortalUtil;
import lumis.util.TextUtil;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.util.string.AppendingStringBuffer;

/**
 * Renders HEAD HTML fragment required by the Portal Studio.
 *
 * @version $Revision: 13160 $ $Date: 2011-06-21 18:50:57 -0300 (Tue, 21 Jun 2011) $
 * @since 6.0.0
 */
@SuppressWarnings("serial")
public class PortalStudioHeadPanel extends Panel
{
	public PortalStudioHeadPanel(String id)
	{
		super(id);
	}

	@Override
	protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag)
	{
		// output legacy head (code that was not brought to this component for cost reduction)
		getResponse().write(renderPortalStudioLegacyHead((LayoutFilePage)getPage()));
		
		// output panel markup
		super.onComponentTagBody(markupStream, openTag);
	}
	
	/**
	 * Renders the Portal Studio head.
	 * @param page page to render the head
	 * @return the html with the page's head.
	 * @since 6.0.0
	 */
	private CharSequence renderPortalStudioLegacyHead(LayoutFilePage page)
	{
		try
		{
			AppendingStringBuffer result = new AppendingStringBuffer(4000);
			
			PageConfig pageConfig = page.getPageWebResource().getPage();
			SessionConfig sessionConfig = SessionConfig.getCurrentSessionConfig();
			ChannelConfig portalAdminCategoryChannelConfig;

			result.append(PortalUtil.generateClientScript(generateAdminClientLocalizer(page)));
			
			if (page.getRootChannelId().equals(ChannelConfig.LUMIS_ADMIN_CHANNEL_ID))
			{
				result.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"lumis/portal/client/stylesheet/portal.css\" charset=\"UTF-8\" />");
			}
			result.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"lumis/portalstudio/client/stylesheet/portalF12.css\" charset=\"UTF-8\"/>");
			
			result.append("<link rel=\"stylesheet\" href=\"lumis/portalstudio/client/stylesheet/portalmode.css\" type=\"text/css\" media=\"screen\" />");
			result.append("<!--[if IE 6]><link rel=\"stylesheet\" href=\"lumis/portalstudio/client/stylesheet/portalmode_ie6.css\" type=\"text/css\" media=\"screen\" /><![endif]-->");
			result.append("<!--[if IE 7]><link rel=\"stylesheet\" href=\"lumis/portalstudio/client/stylesheet/portalmode_ie7.css\" type=\"text/css\" media=\"screen\" /><![endif]-->");
			result.append("<!--[if IE 8]><link rel=\"stylesheet\" href=\"lumis/portalstudio/client/stylesheet/portalmode_ie8.css\" type=\"text/css\" media=\"screen\" /><![endif]-->");
			
			if (pageConfig.getType() == PageConfig.PAGE_TYPE_ADMIN || page.getRootChannelId().equals(ChannelConfig.LUMIS_ADMIN_CHANNEL_ID))
				result.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"lumis/portal/client/stylesheet/portaladmin.css\" charset=\"UTF-8\"/>");

			if (page.getRootChannelId().equals(ChannelConfig.LUMIS_ADMIN_CHANNEL_ID))
			{
				if (page.getParentChannelIds().size() > 2)
				{
					portalAdminCategoryChannelConfig = ManagerFactory.getChannelManager().get(sessionConfig, page.getParentChannelIds().get(2), PortalTransactionFactory.getCurrentTransaction());

					if (portalAdminCategoryChannelConfig.getPosition() == 1)
						result.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"lumis/portalstudio/client/stylesheet/portalF12cat2.css\"  charset=\"UTF-8\"/>");
					else if (portalAdminCategoryChannelConfig.getPosition() == 2)
						result.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"lumis/portalstudio/client/stylesheet/portalF12cat3.css\" charset=\"UTF-8\"/>");
					else if (portalAdminCategoryChannelConfig.getPosition() == 3)
						result.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"lumis/portalstudio/client/stylesheet/portalF12cat4.css\" charset=\"UTF-8\"/>");
				}
			}
			
			return result;
		}
		catch (PortalException e) 
		{
			throw new RuntimeException(e);
		}
	}

	/**
	 * Generates the locale of a given admin interface.
	 * @param the layout file page.
	 * @return the html containing the locales
	 * @throws PortalException
	 * @since 6.0.0
	 */
	private String generateAdminClientLocalizer(LayoutFilePage page) throws PortalException
	{
		String[] clientStringIds = { "STR_SERVICE", "STR_ADD_SERVICE_INSTANCE", "STR_SERVICE_INTERFACE_HOLDER", "STR_PROPERTIES", "STR_ADD_CHANNEL", "STR_ADD_PAGE", "STR_DELETE_CHANNEL", "STR_DELETE_PAGE", "STR_DELETE", "STR_TEMPLATE", "STR_REFRESH", "STR_MANAGE_SERVICES", "STR_MANAGE_SERVICE_INSTANCES", "STR_MANAGE_PAGES", "STR_MANAGE_ACCESS", "STR_MANAGE_SUBCHANNELS", "STR_MANAGE_TEMPLATES", "STR_MANAGE_PAGE_TEMPLATES", "STR_MANAGE_CHANNEL_TEMPLATES", "STR_CANNOT_F12_WHEN_EDITING_PAGE", "STR_STYLE",
				"STR_ADD_PAGE_TEMPLATE", "STR_APPLY_PAGE_TEMPLATE", "STR_UPDATE_PAGES", "STR_DELETE_PAGE_TEMPLATE", "STR_MANAGE_PORTAL_ACCESS", "STR_CHANNEL", "STR_CHANGE_CHANNEL", "STR_NAVIGATOR", "STR_IMPORT_CHANNEL", "STR_EXPORT_CHANNEL", "STR_ARE_YOU_SURE_TO_DELETE", "STR_AREA_PROPERTIES", "STR_COLUMN_PROPERTIES", "STR_ADD_CHANNEL_TEMPLATE", "STR_DELETE_CHANNEL_TEMPLATE", "STR_UPDATE_CHANNELS", "STR_CLEAR_CACHE", "STR_ARE_YOU_SURE_TO_CLEAR_PAGE_CACHE", "STR_ARE_YOU_SURE_TO_CLEAR_CHANNEL_CACHE", "STR_ARE_YOU_SURE_TO_CLEAR_PAGE_TEMPLATE_CACHE", "STR_ARE_YOU_SURE_TO_CLEAR_CHANNEL_TEMPLATE_CACHE" };

		ILocalizationManager localizationManager = ManagerFactory.getLocalizationManager();
		Locale locale = page.getLocale();
		StringBuilder localizer = new StringBuilder(3500);
		localizer.append("<script type=\"text/javascript\">");
		localizer.append("var lum_stringTable = new Array();");

		if (clientStringIds != null)
		{
			for (String stringId : clientStringIds)
			{
				localizer.append("lum_stringTable[\"");
				localizer.append(stringId);
				localizer.append("\"] = \"");
				localizer.append(TextUtil.stringReplace(localizationManager.localize(stringId + ";" + ILocalizationManager.DO_NOT_RENDER_PARAMETERS, PortalAdminStringResource.getInstance(), locale),"\"", "\\\""));
				localizer.append("\";");
			}
		}
		localizer.append("</script>");

		return localizer.toString();
	}


}
